#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++#
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++#
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++#
#  File-Name:   gerverDuellLown_replicationFile.r   		       		
#  Author:      DD 					    			      						
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++#
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++#
# Install and load packages ----
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++#
pkgs <- c('ggplot2','tidyverse','cregg','grid','gridExtra','dplyr','conflicted',
          'xtable','estimatr','ggstance','magrittr','Hmisc','texreg')
# Uncomment line below if packages need to be installed first
# for (pkg in pkgs) install.packages(pkg, character.only = TRUE)
for (pkg in pkgs) library(pkg, character.only = TRUE)
conflict_prefer("filter", "dplyr")
conflict_prefer("select", "dplyr")
conflict_prefer("recode", "dplyr")
conflict_prefer('mutate', "dplyr")
conflict_prefer("summarise", "dplyr")
conflict_prefer("filter", "dplyr")
conflict_prefer("lag", "dplyr")

#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++#
# Load data ----
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++#
data_study1And2 <- read.csv('immigrationEthics_data_study1And2.csv',
  stringsAsFactors=T) %>%
  mutate(
    sample=factor(sample,levels=c('United States','United Kingdom')),
    reasondeny=factor(reasondeny,levels=c('Unemployed','Immigration Quotas',
      'COVID-19 Positive','Criminal History','Terror Watchlist')),
    consequencedenystrength=factor(consequencedenystrength,levels=c('No Harm',
      'Consequence Unknown','Harm','Death')),
    conseqdenycollapse=factor(conseqdenycollapse,levels=c('No Harm',
      'Unknown Harm','Active Harm','Passive Harm')))

data_study3 <- read.csv('immigrationEthics_data_study3.csv',
  stringsAsFactors=T) %>%
  mutate(sourceHarm=factor(sourceHarm,levels=c('Government','Airport Security',
      'Smugglers','Nature')),
    e3harmDeath=factor(e3harmDeath,levels=c('Harm','Death')),
    e3conseq=factor(e3conseq,levels=c('No Harm','Active Harm','Passive Harm')),
    e3deny=factor(e3deny,levels=c('Unemployed','Immigration Quotas',
      'COVID−19 Positive','Criminal History','Terror Watchlist')),
    e3noHarmHarmDeath=factor(e3noHarmHarmDeath,
      levels=c('No Harm','Harm','Death')))

#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++#
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++#
# Main text ----
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++#
# Number of respondents by study ----
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++#
data_study1And2 %>% group_by(sample) %>%
  summarise(N=length(unique(resid)))

data_study3 %>% 
  summarise(N=length(unique(resid)))

#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++#
# Figure 1: ----
# Marginal mean of how reasonable it is to deny a given migrant entry to the 
# country by the reason why they migrated and whether migrants would not be 
# harmed, non-lethally harmed, or killed through deportation (Study 1 and 2).
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++#
results <- data_study1And2 %>%
  do(cj(.,conjresponse ~ reasonmigr, id=~resid, by=~sample + conseqdenycollapse, 
        estimate='mm'))

pdf('figure1.pdf',height=2)
results %>% 
  filter(!(level=='Economic Opportunity'&conseqdenycollapse=='Passive Harm')) %>%
  mutate(conseqdenycollapse=factor(conseqdenycollapse,
    levels=c('No Harm','Unknown Harm','Passive Harm','Active Harm'))) %>%
  ggplot(aes(x=level,y=estimate,ymin=lower,ymax=upper,
             fill=conseqdenycollapse,color=conseqdenycollapse)) + 
  facet_grid(feature~sample,scale='free_y',space='free') +
  geom_pointrange(position=position_dodge2(width=.6),size=.2,shape=21) +
  coord_flip() + 
  scale_y_continuous(limits=c(2.7,6.6)) +
  scale_color_manual(values=c('gray','gray','black','black')) +
  scale_fill_manual(values=c('white','gray','white','black')) +
  labs(x='',y='Denying migrant entry is reasonable') +
  theme_bw() +
  theme(legend.position='right',legend.title=element_blank(),
        strip.background=element_blank(),
        strip.text.y=element_blank())
dev.off()

# statistics in the text around figure 1
out <- data_study1And2 %>% 
  mutate(
    forced=factor(ifelse(reasonmigr=='Economic Opportunity','Forced','Not forced')),
    harm=factor(ifelse(conseqdenycollapse=='No Harm','No harm','Harm'))) 

out %>% group_by(sample) %>%
  do(lm_robust(conjresponse~forced*harm,cluster=resid,data=.) %>% tidy()) %>% 
  filter(term=='forcedNot forced:harmNo harm')

#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++#
# Figure 2: ----
# Marginal mean of how reasonable it is to deny a given migrant entry to the 
# country by the reason for which entry was denied and whether migrants would 
# not be harmed, non-lethally harmed, or killed through deportation. 
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++#
results <- data_study1And2 %>%
  do(cj(.,conjresponse ~ reasondeny, id=~resid, by=~sample + 
          consequencedenystrength, estimate='mm'))

pdf('figure2.pdf',height=2)
results %>% 
  mutate(consequencedenystrength=factor(consequencedenystrength,
    levels=c('No Harm','Consequence Unknown','Harm','Death'))) %>%
  ggplot(aes(x=level,y=estimate,ymin=lower,ymax=upper,
             fill=consequencedenystrength,color=consequencedenystrength)) + 
  facet_grid(feature~sample,scale='free_y',space='free') +
  geom_pointrange(position=position_dodge2(width=.6),size=.2,shape=21) +
  coord_flip() + 
  scale_color_manual(values=c('gray','gray','black','black')) +
  scale_fill_manual(values=c('white','gray','white','black')) +
  scale_y_continuous(limits=c(2.7,6.6)) +
  labs(x='',y='Denying migrant entry is reasonable') +
  theme_bw() +
  theme(legend.position='right',legend.title=element_blank(),
        strip.background=element_blank(),
        strip.text.y=element_blank())
dev.off()

#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++#
# Figure 3: ----
# Marginal mean of how reasonable it is to deny a given migrant entry to the 
# country by whether migrants would not be harmed, non-lethally harmed, or 
# killed through deportation.
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++#
results <- data_study1And2 %>%
  do(cj(.,conjresponse ~ consequencedenystrength, id=~resid, by=~sample, 
        estimate='mm'))

pdf('figure3.pdf',height=1.5)
results %>% 
  ggplot(aes(x=level,y=estimate,ymin=lower,ymax=upper)) + 
  facet_grid(feature~sample,scale='free_y',space='free') +
  geom_pointrange(position=position_dodge2(width=.6),size=.2) +
  coord_flip() + 
  scale_y_continuous(limits=c(3.5,5.7)) +
  labs(x='',y='Denying migrant entry is reasonable') +
  theme_bw() +
  theme(legend.position='none',
        strip.background=element_blank(),
        strip.text.y=element_blank())
dev.off()

#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++#
# Figure 4: ----
# Marginal mean of how reasonable it is to deny a given migrant entry to the 
# country by whether migrants would be harmed and whether that harm would result 
# from deportation("active harm") or circumstances in the country of origin  
# ("passive  harm").
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++#
results <- data_study1And2 %>%
  cj(.,conjresponse ~ conseqdenycollapse, id=~resid, by=~sample, estimate='mm')

pdf('figure4.pdf',height=1.5)
results %>%
  ggplot(aes(x=level,y=estimate,ymin=lower,ymax=upper)) + 
  facet_grid(feature~sample,scale='free_y',space='free') +
  geom_pointrange(position=position_dodge2(width=.6),size=.2) +
  coord_flip() + 
  scale_y_continuous(limits=c(3.5,5.7)) +
  labs(x='',y='Denying migrant entry is reasonable',color='Probability of harm',
       fill='Probability of harm') +
  theme_bw() +
  theme(legend.position='none',
        strip.background=element_blank(),
        strip.text.y=element_blank())
dev.off()

#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++#
# Figure 5: ----
# Marginal mean of how reasonable it is to deny a given migrant entry to the 
# country by the reason for which entry was denied and by whether migrants 
# would be harmed and whether that harm would result from deportation 
# ("active harm") or circumstances in the country of origin ("passive harm"). 
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++#
results <- data_study1And2 %>%
  do(cj(.,conjresponse ~ reasondeny, id=~resid, by=~sample + conseqdenycollapse, 
        estimate='mm'))

pdf('figure5.pdf',height=2)
results %>% 
  mutate(conseqdenycollapse=factor(conseqdenycollapse,
    levels=c('No Harm','Unknown Harm','Passive Harm','Active Harm'))) %>%
  ggplot(aes(x=level,y=estimate,ymin=lower,ymax=upper,
             fill=conseqdenycollapse,color=conseqdenycollapse)) + 
  facet_grid(~sample,scale='free_y',space='free') +
  geom_pointrange(position=position_dodge2(width=.6),size=.2,shape=21) +
  coord_flip() + 
  scale_color_manual(values=c('gray','gray','black','black')) +
  scale_fill_manual(values=c('white','gray','white','black')) +
  scale_y_continuous(limits=c(2.7,6.7)) +
  labs(x='',y='Denying migrant entry is reasonable') +
  theme_bw() +
  theme(legend.position='right',legend.title=element_blank(),
        strip.background=element_blank(),
        strip.text.y=element_text(angle=0))
dev.off()

#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++#
# Figure 6: ----
# Marginal mean of support for government action against migrant by source of 
# harm and (Study 3) 
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++#
results <- cj(
  data_study3 %>%
  filter(!is.na(sourceHarm)&e3harmDeath!='No harm') %>% droplevels(),
  e3support~sourceHarm,id=~resid,by=~forced+e3harmDeath, estimate='mm') 

pdf('figure6.pdf',height=1.5)
results %>% 
  ggplot(aes(x=level,y=estimate,ymin=lower,ymax=upper,fill=forced,
             color=forced)) + 
  geom_pointrange(position=position_dodge2(width=.6),size=.2) +
  facet_grid(~e3harmDeath) +
  coord_flip() + 
  scale_color_manual(values=c('gray','black')) +
  scale_fill_manual(values=c('gray','black')) +
  labs(x='',y='Denying migrant entry is reasonable') +
  theme_bw() +
  theme(legend.position='right',legend.title=element_blank(),
        strip.background=element_blank(),
        strip.text.y=element_text(angle=0))
dev.off()

#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++#
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++#
# Appendix ----
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++#
# Table B.2: ----
# Linear least squares regression of our outcome measure, the response to the 
# question whether excluding a migrant is reasonable, on indicators variables 
# of all attribute levels and a variable capturing the vignette number (recall 
# every respondent sees 5 immigration cases) with standard errors clustered at 
# the respondent-level).
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++#
m <- data_study1And2 %>% 
  mutate(vignette=factor(vignette)) %>%
  group_by(sample) %>% 
  do(m=lm_robust(conjresponse ~ conseqdenycollapse + reasonmigr + 
    reasondeny + methoddeny + proactive + region + gender + vignette, 
    data=., clusters=resid))
  
texreg(list(m$m[[1]],m$m[[2]]),
  file='tableb2.tex',digits=3, single.row=T, include.ci=F,
  custom.coef.names=c(
    'Constant','Unknown harm','Active harm','Passive harm', 
    'Ethnic Persecution','Extreme Poverty','Medical Treatment',
    'Immigration Quotas','COVID-19 Positive','Criminal History',
    'Terror Watchlist','Detention','Retroactive','Asia',
    'Central/South America','Eastern Europe','Middle East',
    'Male','Vignette 2','Vignette 3','Vignette 4','Vignette 5'))

#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++#
# Table B.3: ----
# Linear least squares regression of our outcome measure on indicators variables 
# of all attribute levels and vignette number run separately for the UK and US 
# samples and the attribute levels of reason for migration; standard errors 
# clustered at the respondent-level. The table omits the coefficients on the 
# intercept, all attributes except the indicator on the consequences of 
# enforcement (No harm, harm unknown, active harm, passive harm), and vignette 
# number for ease of display. Recall that there is no permutation in which 
# economic migrants were subject to passive harm, as discussed in the 
# experimental design section above. The regression presented here speaks to 
# the analysis discussed with Figure 1.
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++#
m <- data_study1And2 %>% 
  filter(!(reasonmigr=='Economic Opportunity'&conseqdenycollapse=='Passive Harm')) %>%
  mutate(vignette=factor(vignette)) %>%
  group_by(reasonmigr,sample) %>% 
  do(m=lm_robust(conjresponse ~ conseqdenycollapse + reasondeny + methoddeny + 
    proactive + region + gender + vignette, data=., clusters=resid)) 

coef.names1 <- c('Unknown harm','Active harm')
coef.names2 <- c('Unknown harm','Active harm','Passive harm')
omit.coef <- '(Intercept)|reasondenyImmigration Quotas|reasondenyCOVID-19 Positive|reasondenyCriminal History|reasondenyTerror Watchlist|methoddenyDetention|proactiveRetroactive|regionAsia|regionC&S America|regionEastEur|regionMidEast|genderMale|vignette2|vignette3|vignette4|vignette5'
custom.model.names <- c('United States','United Kingdom') 

texreg(list(m$m[[1]],m$m[[2]]),
       file='tableb3_1.tex', digits=3, single.row=T, include.ci=F,
       custom.coef.names=coef.names1, omit.coef=omit.coef,
       custom.model.names=custom.model.names)

texreg(list(m$m[[3]],m$m[[4]]),
       file='tableb3_2.tex', digits=3, single.row=T, include.ci=F,
       custom.coef.names=coef.names2, omit.coef=omit.coef,
       custom.model.names=custom.model.names)

texreg(list(m$m[[5]],m$m[[6]]),
       file='tableb3_3.tex', digits=3, single.row=T, include.ci=F,
       custom.coef.names=coef.names2, omit.coef=omit.coef,
       custom.model.names=custom.model.names)

texreg(list(m$m[[7]],m$m[[8]]),
       file='tableb3_4.tex', digits=3, single.row=T, include.ci=F,
       custom.coef.names=coef.names2, omit.coef=omit.coef,
       custom.model.names=custom.model.names)

#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++#
# Table B.4: ----
# Linear least squares regression of our outcome measure on indicators variables 
# of all attribute levels and vignette number run separately for the UK and US 
# samples and the attribute levels of reason for visa denial; standard errors 
# clustered at the respondent-level. The table omits the coefficients on the 
# intercept, all attributes except the severity of the consequences of 
# enforcement, and vignette number for ease of display. Recall that there is no 
# permutation in which economic migrants were subject to passive harm, as 
# discussed in the experimental design section above. The regression presented 
# here speaks to the analysis discussed with Figure 2.
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++#
m <- data_study1And2 %>% 
  mutate(vignette=factor(vignette)) %>%
  group_by(reasondeny,sample) %>% 
  do(m=lm_robust(conjresponse ~ consequencedenystrength + methoddeny + 
    reasonmigr + proactive + region + gender + vignette, data=., clusters=resid)) 

coef.names <- c('Consequence Unknown','Harm','Death')
omit.coef <- '(Intercept)|reasonmigrEconomic Opportunity|reasonmigrEthnic Persecution|reasonmigrExtreme Poverty|reasonmigrMedical Treatment|methoddenyDetention|proactiveRetroactive|regionAsia|regionC&S America|regionEastEur|regionMidEast|genderMale|vignette2|vignette3|vignette4|vignette5'
custom.model.names <- c('United States','United Kingdom') 

texreg(list(m$m[[1]],m$m[[2]]),
       file='tableb4_1.tex', digits=3, single.row=T, include.ci=F,
       custom.coef.names=coef.names, omit.coef=omit.coef,
       custom.model.names=custom.model.names)

texreg(list(m$m[[3]],m$m[[4]]),
       file='tableb4_2.tex', digits=3, single.row=T, include.ci=F,
       custom.coef.names=coef.names, omit.coef=omit.coef,
       custom.model.names=custom.model.names)

texreg(list(m$m[[5]],m$m[[6]]),
       file='tableb4_3.tex', digits=3, single.row=T, include.ci=F,
       custom.coef.names=coef.names, omit.coef=omit.coef,
       custom.model.names=custom.model.names)

texreg(list(m$m[[7]],m$m[[8]]),
       file='tableb4_4.tex', digits=3, single.row=T, include.ci=F,
       custom.coef.names=coef.names, omit.coef=omit.coef,
       custom.model.names=custom.model.names)

texreg(list(m$m[[9]],m$m[[10]]),
       file='tableb4_5.tex', digits=3, single.row=T, include.ci=F,
       custom.coef.names=coef.names, omit.coef=omit.coef,
       custom.model.names=custom.model.names)

#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++#
# Table B.5: ----
# Linear least squares regression of our outcome measure on indicators variables 
# of all attribute levels and vignette number run separately for the UK and US 
# sample; standard errors clustered at the respondent-level. We show the 
# coefficient on the indicator for the strength of harm from enforcement 
# (No harm, unknown consequences, harm, or death). The table omits the 
# coefficients on the intercept, the remaining attributes, and vignette 
# number for ease of display. The regression presented here speaks to the 
# analysis discussed with Figure 3.
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++#
m <- data_study1And2 %>% 
  mutate(vignette=factor(vignette)) %>%
  group_by(sample) %>% 
  do(m=lm_robust(conjresponse ~ consequencedenystrength + reasonmigr + 
    reasondeny + methoddeny + proactive + region + gender + vignette, 
    data=., clusters=resid))
  
coef.names <- c('Consequence Unknown','Harm','Death')
omit.coef <- '(Intercept)|reasondenyImmigration Quotas|reasondenyCOVID-19 Positive|reasondenyCriminal History|reasondenyTerror Watchlist|reasonmigrEconomic Opportunity|reasonmigrEthnic Persecution|reasonmigrExtreme Poverty|reasonmigrMedical Treatment|methoddenyDetention|proactiveRetroactive|regionAsia|regionC&S America|regionEastEur|regionMidEast|genderMale|vignette2|vignette3|vignette4|vignette5'
custom.model.names <- c('United States','United Kingdom') 
  
texreg(list(m$m[[1]],m$m[[2]]),
       file='tableb5.tex', digits=3, single.row=T, include.ci=F,
       custom.coef.names=coef.names, omit.coef=omit.coef,
       custom.model.names=custom.model.names)

#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++#
# Table B.6: ----
# Linear least squares regression of our outcome measure on indicators variables 
# of all attribute levels and vignette number run separately for the UK and US 
# sample; standard errors clustered at the respondent-level. We show the 
# coefficient on the indicator for the consequence from enforcement 
# (No harm, unknown harm, active harm, passive harm). The table omits the 
# coefficients on the intercept, the remaining attributes, and vignette 
# number for ease of display. The regression presented here speaks to the 
# analysis discussed with Figure 4.
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++#
m <- data_study1And2 %>% 
  mutate(vignette=factor(vignette)) %>%
  group_by(sample) %>% 
  do(m=lm_robust(conjresponse ~ conseqdenycollapse + reasonmigr + reasondeny + 
    methoddeny + proactive + region + gender + vignette, data=., clusters=resid))

coef.names <- c('Unknown harm','Active harm','Passive harm')                          
omit.coef <- '(Intercept)|reasondenyImmigration Quotas|reasondenyCOVID-19 Positive|reasondenyCriminal History|reasondenyTerror Watchlist|reasonmigrEconomic Opportunity|reasonmigrEthnic Persecution|reasonmigrExtreme Poverty|reasonmigrMedical Treatment|methoddenyDetention|proactiveRetroactive|regionAsia|regionC&S America|regionEastEur|regionMidEast|genderMale|vignette2|vignette3|vignette4|vignette5'
custom.model.names <- c('United States','United Kingdom') 

texreg(list(m$m[[1]],m$m[[2]]),
       file='tableb6.tex', digits=3, single.row=T, include.ci=F,
       custom.coef.names=coef.names, omit.coef=omit.coef,
       custom.model.names=custom.model.names)

#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++#
# Table B.7: ----
# Linear least squares regression of our outcome measure on indicators variables 
# of all attribute levels and vignette number run separately for the UK and US 
# samples and the attribute levels of reason for visa denial; standard errors 
# clustered at the respondent-level. The table omits the coefficients on the 
# intercept, all attributes except for the consequence from enforcement 
# (No harm, unknown harm, active harm, passive harm), and vignette number for 
# ease of display. Recall that there is no permutation in which economic migrants 
# were subject to passive harm, as discussed in the experimental design section 
# above. The regression presented here speaks to the analysis discussed with 
# Figure 5.
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++#
m <-  data_study1And2 %>% 
  mutate(vignette=factor(vignette)) %>%
  group_by(reasondeny,sample) %>% 
  do(m=lm_robust(conjresponse ~ conseqdenycollapse + methoddeny + reasonmigr + 
    proactive + region + gender + vignette, data=., clusters=resid))

coef.names <- c('Unknown harm','Active harm','Passive harm')
omit.coef <- '(Intercept)|reasondenyImmigration Quotas|reasondenyCOVID-19 Positive|reasondenyCriminal History|reasondenyTerror Watchlist|reasonmigrEconomic Opportunity|reasonmigrEthnic Persecution|reasonmigrExtreme Poverty|reasonmigrMedical Treatment|methoddenyDetention|proactiveRetroactive|regionAsia|regionC&S America|regionEastEur|regionMidEast|genderMale|vignette2|vignette3|vignette4|vignette5'
custom.model.names <- c('United States','United Kingdom') 

texreg(list(m$m[[1]],m$m[[2]]),
       file='tableb7_1.tex', digits=3, single.row=T, include.ci=F,
       custom.coef.names=coef.names, omit.coef=omit.coef,
       custom.model.names=custom.model.names)

texreg(list(m$m[[3]],m$m[[4]]),
       file='tableb7_2.tex', digits=3, single.row=T, include.ci=F,
       custom.coef.names=coef.names, omit.coef=omit.coef,
       custom.model.names=custom.model.names)

texreg(list(m$m[[5]],m$m[[6]]),
       file='tableb7_3.tex', digits=3, single.row=T, include.ci=F,
       custom.coef.names=coef.names, omit.coef=omit.coef,
       custom.model.names=custom.model.names)

texreg(list(m$m[[7]],m$m[[8]]),
       file='tableb7_4.tex', digits=3, single.row=T, include.ci=F,
       custom.coef.names=coef.names, omit.coef=omit.coef,
       custom.model.names=custom.model.names)

texreg(list(m$m[[9]],m$m[[10]]),
       file='tableb7_5.tex', digits=3, single.row=T, include.ci=F,
       custom.coef.names=coef.names, omit.coef=omit.coef,
       custom.model.names=custom.model.names)

#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++#
# Table B.8: ----
# Linear least squares regression of our outcome measure on indicators variables 
# of the source of harm (government, airport security, smugglers, nature) and 
# vignette number run separately for consequence of enforcement and whether the 
# migrant was forced to leave (due to ethnic persecution, extreme poverty, or
# for medical treatment) or left voluntarily (for economic opportunity); 
# standard errors clustered at the respondent-level. The table omits the 
# coefficients on the intercept and vignette number for ease of display. 
# Recall that there is no permutation in which economic migrants 
# were subject to passive harm, as discussed in the experimental design section 
# above. The regression presented here speaks to the analysis discussed with 
# Figure 6.
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++#
m <- data_study3 %>% 
  mutate(vignette=factor(vignette)) %>%
  filter(!is.na(e3harmDeath)) %>%
  group_by(forced,e3harmDeath) %>% 
  do(m=lm_robust(e3support~sourceHarm + vignette,clusters=resid,data=.))

coef.names <- c('Airport Security','Smugglers','Nature')
omit.coef <- '(Intercept)|vignette2|vignette3|vignette4|vignette5'
custom.model.names <- c('Harm','Death') 

texreg(list(m$m[[1]],m$m[[2]]),
  file='tableb8_1.tex', digits=3, single.row=T, include.ci=F,
  custom.coef.names=coef.names, omit.coef=omit.coef,
  custom.model.names=custom.model.names)
texreg(list(m$m[[3]],m$m[[4]]),
       file='tableb8_2.tex', digits=3, single.row=T, include.ci=F,
       custom.coef.names=coef.names, omit.coef=omit.coef,
       custom.model.names=custom.model.names)

#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++#
# Figure B.4: ---- 
# Marginal mean of how reasonable it is to deny a given migrant entry to the 
# country by immigration case attribute and the between-respondent probability 
# of harm treatment. We show 95% confidence bounds computed from standard 
# errors clustered at the respondent-level. The figure omits the country of 
# origin attribute for ease of display but categorizes the country of origin 
# into a region of origin indicator.
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++#
results <- rbind(
  data_study1And2 %>% 
  cj(.,conjresponse ~ reasonmigr + reasondeny + methoddeny +
          proactive + region + gender  + country,
    id=~resid, by= ~sample + outcomelikely, estimate='mm'),
  data_study1And2 %>% 
  filter(outcomelikely!='No info') %>%
  droplevels() %>%
  cj(.,conjresponse ~ conseqdeny, id=~resid, by= ~sample + outcomelikely, 
     estimate='mm'),
  data_study1And2 %>% 
    cj(.,conjresponse ~ outcomelikely,
       id=~resid, by= ~sample, estimate='mm') %>%
    filter(level=='No info') %>%
    mutate(feature='conseqdeny',
           level='Consequence Unknown',
           outcomelikely='No info')) %>%
  mutate(outcomelikely=factor(outcomelikely,levels=c(
    'No info','Small Chance','High Chance','Near Certain','Certain')),
    feature=factor(recode(feature,'reasonmigr'='Reason for\nmigration',
                   'reasondeny'='Reason\nimmigration\ndenied',
                   'methoddeny'='Immigration\nprevented by',
                   'conseqdeny'='Consequence\nof denied\nimmigration',
                   'proactive'='Time of\ndenial','region'='Region of\norigin',
                   'gender'='Gender','country'='Country of\norigin'),
      levels=c('Reason for\nmigration','Reason\nImmigration\ndenied',
        'Immigration\nprevented by','Consequence\nof denied\nimmigration',
        'Time of\ndenial','Region of\norigin','Gender')))

pdf('figureb4.pdf',height=7)
results %>% filter(feature!='Country of\norigin') %>%
  ggplot(aes(x=level,y=estimate,ymin=lower,ymax=upper,fill=outcomelikely,color=outcomelikely)) + 
  facet_grid(feature~sample,scale='free_y',space='free') +
  geom_pointrange(position=position_dodge2(width=.6),size=.2) +
  coord_flip() + 
  scale_color_manual(values=c('gray','steelblue3','blue','darkblue','black')) +
  scale_fill_manual(values=c('gray','steelblue3','blue','darkblue','black')) +
  labs(x='',y='Denying migrant entry is reasonable',color='Probability of harm',
       fill='Probability of harm') +
  theme_bw() +
  theme(legend.position='bottom',
        strip.background=element_blank(),
        strip.text.y=element_text(angle=0))
dev.off()

#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++#
# Figure B.5 ----
# Marginal mean of support for denying given migrants entry to the country by 
# the reason why they migrated and whether migrants would not be harmed, 
# non-lethally harmed, or killed through deportation. 
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++#
results <- data_study3 %>%
  cj(.,e3support ~ e3reason, id=~resid, by=~e3conseq, estimate='mm') 

pdf('figureb5.pdf',height=3)
results %>% filter(feature=='e3reason') %>%
  ggplot(aes(x=level,y=estimate,ymin=lower,ymax=upper,fill=e3conseq,color=e3conseq)) + 
  geom_pointrange(position=position_dodge2(width=.6),size=.2) +
  coord_flip() + 
  scale_color_manual(values=c('gray','steelblue3','blue','darkblue','black')) +
  scale_fill_manual(values=c('gray','steelblue3','blue','darkblue','black')) +
  labs(x='',y='Denying migrant entry is reasonable') +
  theme_bw() +
  theme(legend.position='bottom',legend.title=element_blank(),
        strip.background=element_blank(),
        strip.text.y=element_text(angle=0))
dev.off()

#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++#
# Figure B.6 ----
# Marginal mean of support for denying given migrants entry to the country by 
# the reason for which entry was denied and whether migrants would not be harmed, 
# non-lethally harmed, or killed through deportation. 
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++#
results <- data_study3 %>%
  cj(.,e3support ~ e3deny, id=~resid, by=~e3noHarmHarmDeath, estimate='mm') 

pdf('figureb6.pdf',height=3)
results %>% filter(feature=='e3deny') %>%
  ggplot(aes(x=level,y=estimate,ymin=lower,ymax=upper,fill=e3noHarmHarmDeath,
             color=e3noHarmHarmDeath)) + 
  geom_pointrange(position=position_dodge2(width=.6),size=.2) +
  coord_flip() + 
  scale_color_manual(values=c('gray','steelblue3','blue','darkblue','black')) +
  scale_fill_manual(values=c('gray','steelblue3','blue','darkblue','black')) +
  labs(x='',y='Denying migrant entry is reasonable') +
  theme_bw() +
  theme(legend.position='bottom',legend.title=element_blank(),
        strip.background=element_blank(),
        strip.text.y=element_text(angle=0))
dev.off()

#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++#
# Figure B.7 ----
# Marginal mean of support for denying given migrants entry to the country by 
# whether migrants would not be harmed, non-lethally harmed, or killed through 
# deportation. 
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++#
results <- data_study3 %>%
  cj(.,e3support ~ e3noHarmHarmDeath, id=~resid, estimate='mm') 

pdf('figureb7.pdf',height=2)
results %>% 
  ggplot(aes(x=level,y=estimate,ymin=lower,ymax=upper)) + 
  geom_pointrange(position=position_dodge2(width=.6),size=.2) +
  coord_flip() + 
  labs(x='',y='Denying migrant entry is reasonable') +
  theme_bw()
dev.off()

#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++#
# Figure B.8 ----
# Marginal mean of support for denying given migrants entry to the country by 
# whether migrants would be harmed and whether that harm would result from 
# deportation ("active harm") or circumstances in the country of origin 
# ("passive harm").
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++#
results <- data_study3 %>%
  cj(.,e3support ~ e3conseq, id=~resid, estimate='mm') 

pdf('figureb8.pdf',height=2)
results %>% 
  ggplot(aes(x=level,y=estimate,ymin=lower,ymax=upper)) + 
  geom_pointrange(position=position_dodge2(width=.6),size=.2) +
  coord_flip() + 
  labs(x='',y='Denying migrant entry is reasonable') +
  theme_bw()
dev.off()

#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++#
# Figure B.9 ----
# Marginal mean of support for denying given migrants entry to the country by 
# the reason for which entry was denied and by whether migrants would be harmed 
# and whether that harm would result from deportation ("active harm") or 
# circumstances in the country of origin ("passive harm"). 
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++#
results <- data_study3 %>%
  cj(.,e3support ~ e3deny, id=~resid, by=~e3conseq, estimate='mm') 

pdf('figureb9.pdf',height=3)
results %>% 
  ggplot(aes(x=level,y=estimate,ymin=lower,ymax=upper,fill=e3conseq,
             color=e3conseq)) + 
  geom_pointrange(position=position_dodge2(width=.6),size=.2) +
  coord_flip() + 
  scale_color_manual(values=c('gray','steelblue3','blue','darkblue','black')) +
  scale_fill_manual(values=c('gray','steelblue3','blue','darkblue','black')) +
  labs(x='',y='Denying migrant entry is reasonable') +
  theme_bw() +
  theme(legend.position='bottom',legend.title=element_blank(),
        strip.background=element_blank(),
        strip.text.y=element_text(angle=0))
  dev.off()

#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++#
# Figure C.12 ----
# Figures 1-2, reproduced from the main text, for median split of responses to 
# specific questions about immigration attitudes in the United States
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++#
out <- data_study1And2 %>% 
    mutate(across(c(prioritizeCitizens,tooManyNonRefugees),
      ~factor(ifelse(.x>=median(.x,na.rm=T),'Top\nhalf','Bottom\nhalf')),
      levels=c('Bottom\nhalf','Top\nhalf'))) %>%
    pivot_longer(c(prioritizeCitizens,tooManyNonRefugees)) %>%
    mutate(name=factor(recode(name,'prioritizeCitizens'='Citizen first',
      'tooManyNonRefugees'='Too many'),levels=c('Citizen first','Too many')),
      value=factor(value))
  
testFontSize <- 10
  
fig1 <- out %>% filter(sample=='United States') %>%
    do(cj(.,conjresponse ~ reasonmigr, id=~resid, by=~conseqdenycollapse + name + 
      value, estimate='mm')) %>% 
    filter(!(level=='Economic Opportunity'&conseqdenycollapse=='Passive Harm')) %>%
    mutate(conseqdenycollapse=factor(conseqdenycollapse,
      levels=c('No Harm','Unknown Harm','Passive Harm','Active Harm'))) %>%
    ggplot(aes(x=level,y=estimate,ymin=lower,ymax=upper,
      fill=conseqdenycollapse,color=conseqdenycollapse)) + 
    facet_grid(value~name,scale='free_y',space='free') +
    geom_pointrange(position=position_dodge2(width=.6),size=.2,shape=21) +
    coord_flip() + 
    scale_color_manual(values=c('gray','gray','black','black')) +
    scale_fill_manual(values=c('white','gray','white','black')) +
    scale_y_continuous(limits=c(2,7.1)) +
    labs(x='',y='') +
    theme_bw() +
    theme(legend.position='right',legend.title=element_blank(),
          strip.background=element_blank(),text=element_text(size=testFontSize))
  
fig2 <- out %>% filter(sample=='United States') %>%
    do(cj(.,conjresponse ~ reasondeny, id=~resid, by=~name + 
      consequencedenystrength + value, estimate='mm')) %>% 
    mutate(consequencedenystrength=factor(consequencedenystrength,
      levels=c('No Harm','Consequence Unknown','Harm','Death'))) %>%
    ggplot(aes(x=level,y=estimate,ymin=lower,ymax=upper,
      fill=consequencedenystrength,color=consequencedenystrength)) + 
    facet_grid(value~name,scale='free_y',space='free') +
    geom_pointrange(position=position_dodge2(width=.6),size=.2,shape=21) +
    scale_y_continuous(limits=c(2,7.1)) +
    coord_flip() + 
    scale_color_manual(values=c('gray','gray','black','black')) +
    scale_fill_manual(values=c('white','gray','white','black')) +
    labs(x='',y='Denying migrant entry is reasonable') +
    theme_bw() +
    theme(legend.position='right',legend.title=element_blank(),
      strip.background=element_blank(),text=element_text(size=testFontSize))
  
pdf('figurec12.pdf',height=5)
grid.arrange(fig1,fig2,ncol=1)
dev.off()   
  
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++#
# Figure C.13 ----
# Figures 1-2, reproduced from the main text, for median split of responses to 
# specific questions about immigration attitudes in the United Kingdom
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++#
fig1 <- out %>% filter(sample=='United Kingdom') %>%
  do(cj(.,conjresponse ~ reasonmigr, id=~resid, by=~conseqdenycollapse + name + 
    value, estimate='mm')) %>% 
  filter(!(level=='Economic Opportunity'&conseqdenycollapse=='Passive Harm')) %>%
  mutate(conseqdenycollapse=factor(conseqdenycollapse,
    levels=c('No Harm','Unknown Harm','Passive Harm','Active Harm'))) %>%
  ggplot(aes(x=level,y=estimate,ymin=lower,ymax=upper,
    fill=conseqdenycollapse,color=conseqdenycollapse)) + 
  facet_grid(value~name,scale='free_y',space='free') +
  geom_pointrange(position=position_dodge2(width=.6),size=.2,shape=21) +
  coord_flip() + 
  scale_color_manual(values=c('gray','gray','black','black')) +
  scale_fill_manual(values=c('white','gray','white','black')) +
  scale_y_continuous(limits=c(2,7.1)) +
  labs(x='',y='') +
  theme_bw() +
  theme(legend.position='right',legend.title=element_blank(),
    strip.background=element_blank(),text=element_text(size=testFontSize))
  
fig2 <- out %>% filter(sample=='United Kingdom') %>%
  do(cj(.,conjresponse ~ reasondeny, id=~resid, by=~name + 
    consequencedenystrength + value, estimate='mm')) %>% 
  mutate(consequencedenystrength=factor(consequencedenystrength,
    levels=c('No Harm','Consequence Unknown','Harm','Death'))) %>%
  ggplot(aes(x=level,y=estimate,ymin=lower,ymax=upper,
    fill=consequencedenystrength,color=consequencedenystrength)) + 
  facet_grid(value~name,scale='free_y',space='free') +
  geom_pointrange(position=position_dodge2(width=.6),size=.2,shape=21) +
  scale_y_continuous(limits=c(2,7.1)) +
coord_flip() + 
scale_color_manual(values=c('gray','gray','black','black')) +
scale_fill_manual(values=c('white','gray','white','black')) +
labs(x='',y='Denying migrant entry is reasonable') +
theme_bw() +
theme(legend.position='right',legend.title=element_blank(),
  strip.background=element_blank(),text=element_text(size=testFontSize))
  
pdf('figurec13.pdf',height=5)
grid.arrange(fig1,fig2,ncol=1)
dev.off()   
  
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++#
# Figure C.14 ----
# Figures 1-2 for median split of responses to specific questions about 
# immigration attitudes in study 3
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++#
out <- data_study3 %>%
  mutate(
    across(terroristEnter:strengthensEconomy,
      ~recode(.,'Strongly disagree'=1,'Disagree'=2,'Somewhat disagree'=3,
        'Neither agree nor disagree'=4,'Somewhat agree'=5,'Agree'=6,
        'Strongly agree'=7)),
    across(c(terroristEnter,cultureThreatened,overwhelmServices),
      ~ifelse(.x>=quantile(.x,.75,na.rm=T),'Top quartile','Not top quartile')),
    across(c(immigImportant,strengthensEconomy),
    ~ifelse(.x<=quantile(.x,.25,na.rm=T),'Top quartile','Not top quartile'))) %>%
  pivot_longer(terroristEnter:strengthensEconomy) %>%
  mutate(name=factor(recode(name,
    'terroristEnter'='Terrorist\nenter',
    'cultureThreatened'='Culture is\nthreatened',
    'overwhelmServices'='Services\noverwhelmed',
    'strengthensEconomy'='Economy\nstrenghtened',
    'immigImportant'='Immigration\nimportant'),
    levels=c('Immigration\nimportant','Economy\nstrenghtened',
    'Services\noverwhelmed','Culture is\nthreatened','Terrorist\nenter')),
    value=factor(value)) %>%
  droplevels()

testFontSize <- 10

fig1 <- out %>% 
  do(cj(.,e3support ~ e3reason, id=~resid, by=~e3conseq + name + value, 
        estimate='mm')) %>% 
  ggplot(aes(x=level,y=estimate,ymin=lower,ymax=upper,
             shape=e3conseq,color=e3conseq)) + 
  facet_grid(value~name,scale='free_y',space='free') +
  geom_pointrange(position=position_dodge2(width=.6),size=.4,fatten=1) +
  coord_flip() + 
  scale_color_manual(values=c('gray','black','black')) +
  scale_shape_manual(values=c(20,15,17)) +
  scale_y_continuous(limits=c(2.5,4.5)) +
  labs(x='',y='Denying migrant entry is reasonable') +
  theme_bw() +
  theme(legend.position='bottom',legend.title=element_blank(),
        strip.background=element_blank(),text=element_text(size=testFontSize))

fig2 <- out %>% 
  do(cj(.,e3support ~ e3deny, id=~resid, by=~name + e3noHarmHarmDeath + value, 
        estimate='mm')) %>% 
  ggplot(aes(x=level,y=estimate,ymin=lower,ymax=upper,
             shape=e3noHarmHarmDeath,color=e3noHarmHarmDeath)) + 
  facet_grid(value~name,scale='free_y',space='free') +
  geom_pointrange(position=position_dodge2(width=.6),size=.4,fatten=1) +
  coord_flip() + 
  scale_color_manual(values=c('gray','black','black')) +
  scale_shape_manual(values=c(20,15,17)) +
  scale_y_continuous(limits=c(2.5,4.5)) +
  labs(x='',y='Denying migrant entry is reasonable') +
  theme_bw() +
  theme(legend.position='bottom',legend.title=element_blank(),
        strip.background=element_blank(),text=element_text(size=testFontSize))

pdf('figurec14.pdf',height=7)
grid.arrange(fig1,fig2,ncol=1)
dev.off() 

#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++#
# Figure C.15: ----
# Figures 1-5, reproduced from the main text, for median split of responses to 
# specific questions about immigration attitudes in the United States.
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++#
out <- data_study1And2 %>% 
  mutate(across(c(prioritizeCitizens,tooManyNonRefugees),
    ~factor(ifelse(.x>=median(.x,na.rm=T),'Top\nhalf','Bottom\nhalf')),
    levels=c('Bottom\nhalf','Top\nhalf'))) %>%
  pivot_longer(c(prioritizeCitizens,tooManyNonRefugees)) %>%
  mutate(name=factor(recode(name,'prioritizeCitizens'='Citizen first',
    'tooManyNonRefugees'='Too many'),levels=c('Citizen first','Too many')),
    value=factor(value))

testFontSize <- 10

fig1 <- out %>% filter(sample=='United States') %>%
  do(cj(.,conjresponse ~ reasonmigr, id=~resid, by=~conseqdenycollapse + name + 
          value, estimate='mm')) %>% 
  filter(!(level=='Economic Opportunity'&conseqdenycollapse=='Passive Harm')) %>%
  mutate(conseqdenycollapse=factor(conseqdenycollapse,
    levels=c('No Harm','Unknown Harm','Passive Harm','Active Harm'))) %>%
  ggplot(aes(x=level,y=estimate,ymin=lower,ymax=upper,
    fill=conseqdenycollapse,color=conseqdenycollapse)) + 
  facet_grid(value~name,scale='free_y',space='free') +
  geom_pointrange(position=position_dodge2(width=.6),size=.2,shape=21) +
  coord_flip() + 
  scale_color_manual(values=c('gray','gray','black','black')) +
  scale_fill_manual(values=c('white','gray','white','black')) +
  scale_y_continuous(limits=c(2,7.1)) +
  labs(x='',y='') +
  theme_bw() +
  theme(legend.position='right',legend.title=element_blank(),
    strip.background=element_blank(),text=element_text(size=testFontSize))

fig2 <- out %>% filter(sample=='United States') %>%
  do(cj(.,conjresponse ~ reasondeny, id=~resid, by=~name + 
    consequencedenystrength + value, estimate='mm')) %>% 
  mutate(consequencedenystrength=factor(consequencedenystrength,
    levels=c('No Harm','Consequence Unknown','Harm','Death'))) %>%
  ggplot(aes(x=level,y=estimate,ymin=lower,ymax=upper,
    fill=consequencedenystrength,color=consequencedenystrength)) + 
  facet_grid(value~name,scale='free_y',space='free') +
  geom_pointrange(position=position_dodge2(width=.6),size=.2,shape=21) +
  scale_y_continuous(limits=c(2,7.1)) +
  coord_flip() + 
  scale_color_manual(values=c('gray','gray','black','black')) +
  scale_fill_manual(values=c('white','gray','white','black')) +
  labs(x='',y='Denying migrant entry is reasonable') +
  theme_bw() +
  theme(legend.position='right',legend.title=element_blank(),
    strip.background=element_blank(),text=element_text(size=testFontSize))

fig3 <- out %>% filter(sample=='United States') %>%
  do(cj(.,conjresponse ~ consequencedenystrength, id=~resid, by=~name + value, 
    estimate='mm')) %>% 
  ggplot(aes(x=level,y=estimate,ymin=lower,ymax=upper)) + 
  facet_grid(value~name,scale='free_y',space='free') +
  geom_pointrange(position=position_dodge2(width=.6),size=.2) +
  coord_flip() + 
  scale_y_continuous(limits=c(3.5,5.7)) +
  labs(x='',y='') +
  theme_bw() +
  theme(legend.position='none',strip.background=element_blank(),
    text=element_text(size=testFontSize))

fig4 <- out %>% filter(sample=='United States') %>%
  do(cj(.,conjresponse ~ conseqdenycollapse, id=~resid, by=~name + value, 
    estimate='mm')) %>%
  ggplot(aes(x=level,y=estimate,ymin=lower,ymax=upper)) + 
  facet_grid(value~name,scale='free_y',space='free') +
  geom_pointrange(position=position_dodge2(width=.6),size=.2) +
  coord_flip() + 
  scale_y_continuous(limits=c(3.5,5.7)) +
  labs(x='',y='',color='Probability of harm',
    fill='Probability of harm') +
  theme_bw() +
  theme(legend.position='none',strip.background=element_blank(),
    text=element_text(size=testFontSize))

fig5 <- out %>% filter(sample=='United States') %>%
  do(cj(.,conjresponse ~ reasondeny, id=~resid, by=~name + conseqdenycollapse +
    value, estimate='mm')) %>% 
  mutate(conseqdenycollapse=factor(conseqdenycollapse,
    levels=c('No Harm','Unknown Harm','Passive Harm','Active Harm'))) %>%
  ggplot(aes(x=level,y=estimate,ymin=lower,ymax=upper,
    fill=conseqdenycollapse,color=conseqdenycollapse)) + 
  facet_grid(value~name,scale='free_y',space='free') +
  geom_pointrange(position=position_dodge2(width=.6),size=.2,shape=21) +
  coord_flip() + 
  scale_color_manual(values=c('gray','gray','black','black')) +
  scale_fill_manual(values=c('white','gray','white','black')) +
  scale_y_continuous(limits=c(2,7.1)) +
  labs(x='',y='Denying migrant entry is reasonable') +
  theme_bw() +
  theme(legend.position='right',legend.title=element_blank(),
    strip.background=element_blank(),
    text=element_text(size=testFontSize))

row3 <- grid.arrange(fig3,fig4)
pdf('figurec15.pdf',height=10)
grid.arrange(fig1,fig2,row3,fig5,heights=c(.8,.8,1.4,.8,.8),ncol=1)
dev.off() 

#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++#
# Figure C.16: ----
# Figures 1-5, reproduced from the main text, for median split of responses to 
# specific questions about immigration attitudes in the United Kingdom
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++#
fig1 <- out %>% filter(sample=='United Kingdom') %>%
  do(cj(.,conjresponse ~ reasonmigr, id=~resid, by=~conseqdenycollapse + name + 
    value, estimate='mm')) %>% 
  filter(!(level=='Economic Opportunity'&conseqdenycollapse=='Passive Harm')) %>%
  mutate(conseqdenycollapse=factor(conseqdenycollapse,
    levels=c('No Harm','Unknown Harm','Passive Harm','Active Harm'))) %>%
  ggplot(aes(x=level,y=estimate,ymin=lower,ymax=upper,
    fill=conseqdenycollapse,color=conseqdenycollapse)) + 
  facet_grid(value~name,scale='free_y',space='free') +
  geom_pointrange(position=position_dodge2(width=.6),size=.2,shape=21) +
  coord_flip() + 
  scale_color_manual(values=c('gray','gray','black','black')) +
  scale_fill_manual(values=c('white','gray','white','black')) +
  scale_y_continuous(limits=c(2,7.1)) +
  labs(x='',y='') +
  theme_bw() +
  theme(legend.position='right',legend.title=element_blank(),
    strip.background=element_blank(),text=element_text(size=testFontSize))

fig2 <- out %>% filter(sample=='United Kingdom') %>%
  do(cj(.,conjresponse ~ reasondeny, id=~resid, by=~name + 
    consequencedenystrength + value, estimate='mm')) %>% 
  mutate(consequencedenystrength=factor(consequencedenystrength,
    levels=c('No Harm','Consequence Unknown','Harm','Death'))) %>%
  ggplot(aes(x=level,y=estimate,ymin=lower,ymax=upper,
    fill=consequencedenystrength,color=consequencedenystrength)) + 
  facet_grid(value~name,scale='free_y',space='free') +
  geom_pointrange(position=position_dodge2(width=.6),size=.2,shape=21) +
  scale_y_continuous(limits=c(2,7.1)) +
  coord_flip() + 
  scale_color_manual(values=c('gray','gray','black','black')) +
  scale_fill_manual(values=c('white','gray','white','black')) +
  labs(x='',y='Denying migrant entry is reasonable') +
  theme_bw() +
  theme(legend.position='right',legend.title=element_blank(),
    strip.background=element_blank(),text=element_text(size=testFontSize))

fig3 <- out %>% filter(sample=='United Kingdom') %>%
  do(cj(.,conjresponse ~ consequencedenystrength, id=~resid, by=~name + value, 
    estimate='mm')) %>% 
  ggplot(aes(x=level,y=estimate,ymin=lower,ymax=upper)) + 
  facet_grid(value~name,scale='free_y',space='free') +
  geom_pointrange(position=position_dodge2(width=.6),size=.2) +
  coord_flip() + 
  scale_y_continuous(limits=c(3.5,5.7)) +
  labs(x='',y='') +
  theme_bw() +
  theme(legend.position='none',strip.background=element_blank(),
    text=element_text(size=testFontSize))

fig4 <- out %>% filter(sample=='United Kingdom') %>%
  do(cj(.,conjresponse ~ conseqdenycollapse, id=~resid, by=~name + value, 
    estimate='mm')) %>%
  ggplot(aes(x=level,y=estimate,ymin=lower,ymax=upper)) + 
  facet_grid(value~name,scale='free_y',space='free') +
  geom_pointrange(position=position_dodge2(width=.6),size=.2) +
  coord_flip() + 
  scale_y_continuous(limits=c(3.5,5.7)) +
  labs(x='',y='',color='Probability of harm',
    fill='Probability of harm') +
  theme_bw() +
  theme(legend.position='none',strip.background=element_blank(),
    text=element_text(size=testFontSize))

fig5 <- out %>% filter(sample=='United Kingdom') %>%
  do(cj(.,conjresponse ~ reasondeny, id=~resid, by=~name + conseqdenycollapse +
    value, estimate='mm')) %>% 
  mutate(conseqdenycollapse=factor(conseqdenycollapse,
    levels=c('No Harm','Unknown Harm','Passive Harm','Active Harm'))) %>%
  ggplot(aes(x=level,y=estimate,ymin=lower,ymax=upper,
    fill=conseqdenycollapse,color=conseqdenycollapse)) + 
  facet_grid(value~name,scale='free_y',space='free') +
  geom_pointrange(position=position_dodge2(width=.6),size=.2,shape=21) +
  coord_flip() + 
  scale_color_manual(values=c('gray','gray','black','black')) +
  scale_fill_manual(values=c('white','gray','white','black')) +
  scale_y_continuous(limits=c(2,7.1)) +
  labs(x='',y='Denying migrant entry is reasonable') +
  theme_bw() +
  theme(legend.position='right',legend.title=element_blank(),
    strip.background=element_blank(),
    text=element_text(size=testFontSize))

row3 <- grid.arrange(fig3,fig4)
pdf('figurec16.pdf',height=10)
grid.arrange(fig1,fig2,row3,fig5,heights=c(.8,.8,1.4,.8,.8),ncol=1)
dev.off() 

#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++#
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++#
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++#


